﻿using System;
using System.Net;
using System.Windows.Input;
using CuteRibs.D3Armory.Models;
using CuteRibs.D3Armory.Resources;
using CuteRibs.D3Armory.ViewModels;

namespace CuteRibs.D3Armory.Views
{
	public partial class ProfileAddPage : CuteRibs.D3Armory.Views.PageBase
	{
		private Profile profile;
		private ProfileAddPageViewModel myViewModel;

		public ProfileAddPageViewModel MyViewModel
		{
			get
			{
				if (this.myViewModel == null)
				{
					this.myViewModel = (ProfileAddPageViewModel)this.DataContext;
				}

				return this.myViewModel;
			}
		}

		public ProfileAddPage()
			: base()
		{
			InitializeComponent();

			this.Loaded += (s, e) =>
			{
				this.AppButtons[0].Text = AppRes.btnFind;
				this.AppButtons[1].Text = AppRes.btnSave;
			};

			this.lpRealm.SelectionChanged += (s, e) =>
			{
				if (e.AddedItems != null && e.AddedItems.Count > 0)
				{
					this.tbRealm.DataContext = e.AddedItems[0] as Realm;
				}
			};

			this.tbRealm.Tap += (s, e) =>
			{
				this.lpRealm.Open();
			};

			this.tbTag.KeyDown += (s, e) =>
			{
				if (e.Key == Key.Enter)
				{
					this.tbNumber.Focus();
				}
			};

			this.tbNumber.KeyDown += (s, e) =>
			{
				if (e.Key == Key.Enter)
				{
					this.FindProfile();
				}
			};
		}

		private async void FindProfile()
		{
			var tag = this.tbTag.Text.Trim();
			var number = this.tbNumber.Text.Trim();

			if (string.IsNullOrWhiteSpace(tag))
			{
				this.tbTag.Focus();
				return;
			}

			if (string.IsNullOrWhiteSpace(number))
			{
				this.tbNumber.Focus();
				return;
			}

			this.Focus();
			this.MyViewModel.ShowProgressBar = true;
			var realm = this.lpRealm.SelectedItem as Realm;
			var battleTag = tag + "-" + number;

			try
			{
				foreach (var c in number)
				{
					if (!char.IsNumber(c))
					{
						App.ShowAlert(AppRes.MsgInvalidBattleTag);
						this.MyViewModel.ShowProgressBar = false;
						return;
					}
				}

				Profile profile = await App.GetProfile(realm, battleTag, false);

				if (!string.IsNullOrEmpty(profile.ErrorCode))
				{
					App.ShowAlert(string.Format("{0}: {1}", profile.ErrorCode, profile.ErrorReason));
					this.MyViewModel.ProfileViewModel = null;
					this.AppButtons[1].IsEnabled = false;
				}
				else
				{
					this.MyViewModel.ProfileViewModel = new ProfileViewModel(profile);
					this.AppButtons[1].IsEnabled = true;
					this.lbHero.Focus();
				}

				this.profile = profile;
			}
			catch (Exception ex)
			{
				if (ex is WebException)
				{
					App.ShowAlert(AppRes.MsgDataAccessError);
				}
				else
				{
					App.ShowError(new AppException(ex, realm, battleTag));
				}
			}

			this.MyViewModel.ShowProgressBar = false;
		}

		private void SaveProfile()
		{
			if (this.profile == null)
			{
				App.ShowAlert(AppRes.MsgNoProfile);
				this.AppButtons[1].IsEnabled = false;
			}
			else
			{
				this.Dispatcher.BeginInvoke(delegate
				{
					App.SaveProfile(this.profile);
				});

				App.ShowAlert(AppRes.MsgProfileSave);
			}
		}

		private void btnSearch_Click(object sender, EventArgs e)
		{
			this.FindProfile();
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			this.SaveProfile();
		}
	}
}
